package com.clh.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.clh.common.PageInfo;
import com.clh.dto.Result;
import com.clh.entity.Announcement;
import com.clh.service.AnnouncementService;
import com.clh.service.impl.AnnouncementServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class AnnouncementServlet
 */
@WebServlet("/AnnouncementServlet")
public class AnnouncementServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private AnnouncementService announcementService = new AnnouncementServiceImpl();

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String op = request.getParameter("op");
		//System.out.println(op);
		if ("announcementSearch".equals(op)) {
			doShowAnnouncement(request, response);
		} else if ("announcementSimpleSearch".equals(op)) {
			doSimpleSearch(request, response);
		} else if ("add".equals(op)) {
			doAddAnnouncement(request, response);
		} else if ("findById".equals(op)) {
			doFind(request, response);
		} else if ("update".equals(op)) {
			doUpdateAnnouncement(request, response);
		}

	}

	/**
	 * @param request
	 * @param response
	 */
	private void doFind(HttpServletRequest request, HttpServletResponse response) {
		Announcement announcement = new Announcement();
		List<Announcement> list = new ArrayList<Announcement>();
		Result result = new Result();
		String annoNo = request.getParameter("annoNo");
		System.out.println(annoNo);
		if (annoNo == null || "".equals(annoNo)) {
			result.fail(0, "参数错误");
		} else {
			try {
				announcement.setAnnoNo(annoNo);
				list = announcementService.findByCondition(announcement);
				if (list != null) {
					result.ok(list);
				} else {
					result.fail(0, "没有数据");
				}

				ObjectMapper objectMapper = new ObjectMapper();
				String json = objectMapper.writeValueAsString(result);
				response.setCharacterEncoding("utf-8");
				response.setContentType("application/json;charset=utf-8");
				response.getWriter().write(json);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

	/**
	 * @param request
	 * @param response
	 */
	private void doUpdateAnnouncement(HttpServletRequest request, HttpServletResponse response) {
		Announcement announcement = new Announcement();
		Result result = new Result();
		String annoNo = request.getParameter("annoNo");
		String annoTitle = request.getParameter("annoTitle");
		String annoTime = request.getParameter("annoTime");
		String annoContent = request.getParameter("annoContent");

		if (annoNo == null || "".equals(annoNo) || annoTitle == null || "".equals(annoTitle) || annoTime == null
				|| "".equals(annoTime) || annoContent == null || "".equals(annoContent)) {
			result.fail(0, "参数错误");
		} else {
			try {
				announcement.setAnnoNo(annoNo);
				announcement.setAnnoTitle(annoTitle);
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				Date annoTimeDate = df.parse(annoTime);
				announcement.setAnnoTime(annoTimeDate);
				announcement.setAnnoContent(annoContent);
				boolean flag = announcementService.update(announcement);
				if (flag) {
					result.ok(flag);
				} else {
					result.fail(0, "没有数据");
				}

				ObjectMapper objectMapper = new ObjectMapper();
				String json = objectMapper.writeValueAsString(result);
				response.setCharacterEncoding("utf-8");
				response.setContentType("application/json;charset=utf-8");
				response.getWriter().write(json);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param request
	 * @param response
	 */
	private void doAddAnnouncement(HttpServletRequest request, HttpServletResponse response) {
		Announcement announcement = new Announcement();
		Result result = new Result();
		String annoTitle = request.getParameter("annoTitle");
		String annoTime = request.getParameter("annoTime");
		String annoContent = request.getParameter("annoContent");

		if (annoTitle == null || "".equals(annoTitle) || annoTime == null || "".equals(annoTime) || annoContent == null
				|| "".equals(annoContent)) {
			result.fail(0, "参数错误");
		} else {
			try {
				announcement.setAnnoTitle(annoTitle);
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				Date annoTimeDate = df.parse(annoTime);
				announcement.setAnnoTime(annoTimeDate);
				announcement.setAnnoContent(annoContent);
				boolean flag = announcementService.add(announcement);
				if (flag) {
					result.ok(flag);
				} else {
					result.fail(0, "没有数据");
				}

				ObjectMapper objectMapper = new ObjectMapper();
				String json = objectMapper.writeValueAsString(result);
				response.setCharacterEncoding("utf-8");
				response.setContentType("application/json;charset=utf-8");
				response.getWriter().write(json);

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * @param request
	 * @param response
	 */
	private void doSimpleSearch(HttpServletRequest request, HttpServletResponse response) {
		List<Announcement> list = new ArrayList<Announcement>();
		Result result = new Result();
		try {
			list = announcementService.findAll();
			result.ok(list);
			ObjectMapper objectMapper = new ObjectMapper();
			String json = objectMapper.writeValueAsString(result);
			response.setCharacterEncoding("utf-8");
			response.setContentType("application/json;charset=utf-8");
			response.getWriter().write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void doShowAnnouncement(HttpServletRequest request, HttpServletResponse response) {
		Announcement announcement = new Announcement();
		Result result = new Result();
		try {
			String annoTitle = request.getParameter("annoTitle");
			String startDate = request.getParameter("startDate");
			String endDate = request.getParameter("endDate");
			String pageNum = request.getParameter("pageNum");
			String pageSize = request.getParameter("pageSize");

			// System.out.println("Servlet pageNum = "+pageNum);
			// System.out.println("Servlet pageSize = "+pageSize);

			if (pageNum == null || pageSize == null) {
				result.fail(0, "参数错误");
			} else {
				announcement.setAnnoTitle(annoTitle);
				PageInfo<Announcement> pageInfo = announcementService.findByPage(announcement,
						Integer.parseInt(pageNum), Integer.parseInt(pageSize), startDate, endDate);
				if (!pageInfo.getList().isEmpty()) {
					result.ok(pageInfo);
				} else {
					result.fail(0, "没有数据");
				}
			}
			// System.out.println(annoTitle)
			ObjectMapper objectMapper = new ObjectMapper();
			String json = objectMapper.writeValueAsString(result);
			response.setCharacterEncoding("utf-8");
			response.setContentType("application/json;charset=utf-8");
			response.getWriter().write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
